using System.Collections.Generic;
using GameFramework.Battle.Core;
using Gameplay.PVE.Config;
using Gameplay.PVE.Entity;
using Gameplay.PVE.Utils;
using UnityEngine;

namespace Gameplay.PVE.Skill
{
    public class Area : SkillBullet
    {
        private List<UnitBase> candidates = new List<UnitBase>();
        private List<UnitBase> targetList = new List<UnitBase>();
        private Vector3 startPosition;
        private Vector3 targetForward;
        private Vector3 targetSkillPosition;
        private float startTime;

        private float lastAffectTime;

        private GameObject areaTemp;
        

        public void SetTargetPositionAndForward(Vector3 startPosition,Vector3 targetPosition,Vector3 targetForward)
        {
            this.startPosition = startPosition;
            this.targetSkillPosition = targetPosition;
            this.targetForward = targetForward;
            if (bullet != null)
            {
                bullet.transform.position = startPosition;
            }
            lastPosition = startPosition;
        }

        public override void Initialize(PveSkillBulletConfig config, PveSkillConfig skillConfig,SkillBase skill, Vector3 startPosition, Vector3 offset, Vector3 angle,
            UnitBase source)
        {
            base.Initialize(config, skillConfig,skill, startPosition, offset, angle, source);
            PveTargetSearchHelper.GetCandidates(source,skillConfig,ref candidates);
            startTime = TimeManager.logicTime;
            lastAffectTime = 0;
            if (config.effect_id != 0)
            {
                PveEffectManager.Instance.PlayEffect(config.effect_id, this.targetSkillPosition, skill.targetForward,5);
            }

            if (PveGlobalVlues.TestShowArea)
            {
                areaTemp = GameObject.CreatePrimitive(PrimitiveType.Cube);
                float width = config.director_args["width"];
                float height = config.director_args["height"];
                areaTemp.name = source.Data.id + " " + config.id;
                areaTemp.transform.SetParent(PveScene.pveRootTrans);
                areaTemp.transform.localScale = new Vector3(width,1,height);
                areaTemp.transform.position = this.startPosition + (this.targetForward).normalized * height / 2;
            }
        }

        public override void Dispose(bool isForceDestroy = false)
        {
            if (PveGlobalVlues.TestShowArea)
            {
                if (areaTemp)
                {
                    GameObject.Destroy(areaTemp);
                    areaTemp = null;
                }
            }
            base.Dispose(isForceDestroy);
        }

        public override void CheckStartHit()
        {
            lastAffectTime = TimeManager.logicTime;
            PveTargetSearchHelper.CheckTarget(config,startPosition,targetSkillPosition,targetForward,ref candidates,ref targetList);
            for (int i = 0; i < targetList.Count; i++)
            {
                var target = targetList[i];
                if (target is BattleUnit targetBattleUnit)
                {
                    HitUnit(targetBattleUnit);
                }
                else if (target is ObstacleUnit targetObstacle)
                {
                    HitObstacle(targetObstacle,targetObstacle.Data.position);
                }
            }
        }

        public override void Update(float deltaTime)
        {
            base.Update(deltaTime);
            var time = TimeManager.logicTime - startTime;
            if (time >= config.duration)
            {
                Dispose();
                return;
            }
            if (TimeManager.logicTime - lastAffectTime <= config.element_interval)
            {
               
            }
            else
            {
                lastAffectTime = TimeManager.logicTime;
                PveTargetSearchHelper.CheckTarget(config,startPosition,targetSkillPosition,targetForward,ref candidates,ref targetList);
                for (int i = 0; i < targetList.Count; i++)
                {
                    var target = targetList[i];
                    if (target is BattleUnit targetBattleUnit)
                    {
                        HitUnit(targetBattleUnit);
                    }
                    else if (target is ObstacleUnit targetObstacle)
                    {
                        HitObstacle(targetObstacle,targetObstacle.Data.position);
                    }
                }
            }
        }
    }
}